#include <iostream>
#include<mutex>
#include <thread>
#include<memory>
#include<vector>
#include "prototype.h"

int main(int argc, char *argv[]){
    // 创建原型对象
    ConcretePrototype originalPrototype;

    // 在多个线程中获取原型对象的副本并执行任务
    std::vector<std::unique_ptr<Prototype>> clones;
    std::mutex cloneMutex;

    std::vector<std::thread> threads;
    for (int i = 0; i < 5; ++i) {
        threads.emplace_back([&originalPrototype, &clones, &cloneMutex]() {
            // 获取原型对象的副本
            std::unique_ptr<Prototype> clone = originalPrototype.clone();

            // 执行任务
            if (clone) {
                clone->performTask();
            }

            // 将副本存储在容器中
            std::lock_guard<std::mutex> lock(cloneMutex);
            clones.push_back(std::move(clone));
        });
    }

    // 等待所有线程结束
    for (auto& thread : threads) {
        thread.join();
    }

    return 0;
}
